package view;

import java.awt.Font;

import java.io.*;

import javax.swing.JOptionPane;

/*
 	Wanneer een methode met venster gebruikt is moet het programma afgesloten worden met System.exit(0)
 */

public class IO {

	// INSTANCE VARIABLES
	
	static private BufferedReader bufferedReader = new BufferedReader(
			new InputStreamReader(System.in));
	static private String invalidInput = new String("Invalid input");
	static private String invalidInputTitle = new String("Error");
	
	// METHODS
	
	/** Sets font family to courier */
	public static void defaultFontToCourier(int fontSize) {
		
		// Font size too large
		if (fontSize > 26) {
			fontSize = 26;
		}
		
		// Font size too small
		if (fontSize < 8) {
			fontSize = 8;
		}
		
		// Create default font
		Font font = new Font("Courier", Font.BOLD, fontSize);
		
		// Set default font of UI
		javax.swing.UIManager.put("OptionPane.messageFont", font);
	}

	// CONSOLE I/O METHODS
	
	/** Print a string to console */
	public static void print(String string) {
		System.out.print(string);
	}

	/** Read string */
	public static String readString(String message) {

		String s = "";
		
		System.out.print(message + " ");
		
		try {
			s = bufferedReader.readLine();
		} catch (IOException ioe) {
			System.out.println("I/O error: " + ioe.getClass().toString());
			System.out.println(ioe.getMessage());
		}
		
		return s;
	}

	/** Read char */
	public static char readChar(String message) {
		String s = readString(message);
		char c = s.charAt(0);
		return c;
	}

	/** Read byte */
	public static byte readByte(String message) {
		String s = readString(message);
		byte b = 0;
		try {
			b = Byte.parseByte(s);
		} catch (NumberFormatException nfe) {
			System.out.println(invalidInput);
		}
		return b;
	}

	/** Read short */
	public static short readShort(String message) {
		String s = readString(message);
		short sh = 0;
		try {
			sh = Short.parseShort(s);
		} catch (NumberFormatException nfe) {
			System.out.println(invalidInput);
		}
		return sh;
	}

	/** Read int */
	public static int readInt(String message) {
		String s = readString(message);
		int i = 0;
		try {
			i = Integer.parseInt(s);
		} catch (NumberFormatException nfe) {
			System.out.println(invalidInput);
		}
		return i;
	}

	/** Read long */
	public static long readLong(String message) {
		String s = readString(message);
		long l = 0;
		try {
			l = Long.parseLong(s);
		} catch (NumberFormatException nfe) {
			System.out.println(invalidInput);
		}
		return l;
	}

	/** Read float */
	public static float readFloat(String message) {
		String s = readString(message);
		float f = 0.0f;
		try {
			f = Float.parseFloat(s);
		} catch (NumberFormatException nfe) {
			System.out.println(invalidInput);
		}
		return f;
	}

	/** Read double */
	public static double readDouble(String message) {
		String s = readString(message);
		double d = 0.0;
		try {
			d = Double.parseDouble(s);
		} catch (NumberFormatException nfe) {
			System.out.println(invalidInput);
		}
		return d;
	}

	// GUI I/O METHODS
	
	/** Shows a message dialog */
	public static void messageDialog(String message) {
		JOptionPane.showMessageDialog(null, message);
	}

	/** Show an input dialog to read a string */
	public static String inputDialogString() {
		String s = JOptionPane.showInputDialog(null, "");
		return s;
	}

	/** Show an input dialog to read a string */
	public static String inputDialogString(String message) {
		String s = JOptionPane.showInputDialog(null, message);
		return s;
	}

	/** Show an input dialog to read a string */
	public static String inputDialogString(String message, String title) {
		String s = JOptionPane.showInputDialog(null, message, title, 1);
		return s;
	}

	/** Show an input dialog to correct a string */
	public static String correctionDialogString(String message, String correctionString) {
		String s = JOptionPane.showInputDialog(null, message, correctionString);
		return s;
	}

	/** Show an input dialog to read a character */
	public static char inputDialogChar() {
		String s = inputDialogString();
		char c = s.charAt(0);
		return c;
	}

	/** Show an input dialog to read a character */
	public static char leesCharMetVenster(String message) {
		String s = inputDialogString(message);
		char c = s.charAt(0);
		return c;
	}

	/** Show an input dialog to read a byte */
	public static byte inputDialogByte() {
		String s = inputDialogString();
		byte b = 0;
		try {
			b = Byte.parseByte(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null,	invalidInput, "Fout",
					JOptionPane.ERROR_MESSAGE);
		}
		return b;
	}

	/** Show an input dialog to read a byte */
	public static byte InputDialogByte(String message) {
		String s = inputDialogString(message);
		byte b = 0;
		try {
			b = Byte.parseByte(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return b;
	}

	/** Show an input dialog to read a short */
	public static short leesShortMetVenster() {
		String s = inputDialogString();
		short sh = 0;
		try {
			sh = Short.parseShort(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return sh;
	}

	/** Show an input dialog to read a short */
	public static short leesShortMetVenster(String message) {
		String s = inputDialogString(message);
		short sh = 0;
		try {
			sh = Short.parseShort(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return sh;
	}

	/** Show an input dialog to read an int */
	public static int inputDialogInt() {
		String s = inputDialogString();
		int i = 0;
		try {
			i = Integer.parseInt(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return i;
	}

	/** Show an input dialog to read an int */
	public static int inputDialogInt(String message) {
		String s = inputDialogString(message);
		int i = 0;
		try {
			i = Integer.parseInt(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return i;
	}

	/** Show an input dialog to read an int */
	public static int inputDialogInt(String message, String title) {
		String s = inputDialogString(message, title);
		int i = 0;
		try {
			i = Integer.parseInt(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return i;
	}

	/** Show an input dialog to read a long */
	public static long inputDialogLong() {
		String s = inputDialogString();
		long l = 0;
		try {
			l = Long.parseLong(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return l;
	}

	/** Show an input dialog to read a long */
	public static long inputDialogLong(String message) {
		String s = inputDialogString(message);
		long l = 0;
		try {
			l = Long.parseLong(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return l;
	}

	/** Show an input dialog to read a float */
	public static float inputDialogFloat() {
		String s = inputDialogString();
		float f = 0.0f;
		try {
			f = Float.parseFloat(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return f;
	}

	/** Show an input dialog to read a float */
	public static float inputDialogFloat(String message) {
		String s = inputDialogString(message);
		float f = 0.0f;
		try {
			f = Float.parseFloat(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return f;
	}

	/** Show an input dialog to read a double */
	public static double inputDialogDouble() {
		String s = inputDialogString();
		double d = 0.0;
		try {
			d = Double.parseDouble(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return d;
	}

	/** Show an input dialog to read a double */
	public static double inputDialogDouble(String message) {
		String s = inputDialogString(message);
		double d = 0.0;
		try {
			d = Double.parseDouble(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null,
					"Gelieve een geldige double waarde in te voeren.", "Fout",
					JOptionPane.ERROR_MESSAGE);
		}
		return d;
	}

	/** Show an input dialog to read a double */
	public static double inputDialogDouble(String message, String title) {
		String s = inputDialogString(message, title);
		double d = 0.0;
		try {
			d = Double.parseDouble(s);
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, invalidInput, invalidInputTitle,
					JOptionPane.ERROR_MESSAGE);
		}
		return d;
	}
}